﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TPLOAWebService.Models;

namespace TPLOAWebService.Controllers
{
    public class StaffsController : ApiController
    {
        static readonly IStaffRepository repository = new StaffRepository();

        public IEnumerable<Staff> GetAllStaffs()
        {
            return repository.GetAll();
        }

        public Staff GetStaff(string id)
        {
            Staff item = repository.Get(id);
            if (item == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return item;
        }

        public HttpResponseMessage PostStaff(Staff item)
        {
            item = repository.Add(item);
            var response = Request.CreateResponse<Staff>(HttpStatusCode.Created, item);

            string uri = Url.Link("DefaultApi", new { id = item.StaffID });
            response.Headers.Location = new Uri(uri);
            return response;
        }

        public void PutStaff(string id, Staff staff)
        {
            staff.StaffID = id;
            if (!repository.Update(staff))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        public HttpResponseMessage DeleteStaffs(string id)
        {
            repository.Remove(id);
            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
    }
}
